﻿package com.mui.controls
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class GImageBitmap extends Bitmap implements IFrUI
    {
        private var _imgUrl:String;
        private var _loadPriority:int = 1;
        private var _isDisposed:Boolean = false;
        public var onLoadCompl:Function;
        protected var _info:Object;
        public static var _arySuffix:Array = [".png", ".jpg", ".jpeg", ".swf"];
        private static var _dicUrlRefer:Dictionary = new Dictionary();

        public function GImageBitmap(param1:int = 0, param2:int = 0)
        {
            x = param1;
            y = param2;
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        private function onAddToStage(event:Event) : void
        {
            return;
        }// end function

        private function onRemoveFromStage(event:Event) : void
        {
            return;
        }// end function

        public function set imgUrl(param1:String) : void
        {
            if (this._imgUrl)
            {
                LoaderManager.instance.removeResourceEvent(this._imgUrl, this.loadUrlCompl);
            }
            if (!param1)
            {
                removeReference(this._imgUrl);
                this._imgUrl = "";
                bitmapData = null;
                return;
            }
            removeReference(this._imgUrl);
            this._imgUrl = getUrl(param1);
            addReference(this._imgUrl);
            this.setBitmapdata(this._imgUrl);
            return;
        }// end function

        public function get imgUrl() : String
        {
            return this._imgUrl;
        }// end function

        protected function setBitmapdata(param1:String) : void
        {
            LoaderManager.instance.load(param1, this.loadUrlCompl, this.loadPriority);
            return;
        }// end function

        protected function loadUrlCompl(param1) : void
        {
            if (this._isDisposed)
            {
                return;
            }
            this._info = param1;
            if (this.onLoadCompl != null)
            {
                Global.instance.callLater(this.clllLaterHandler);
            }
            else
            {
                this.bitmapData = this._info.bitmapData as BitmapData;
            }
            return;
        }// end function

        protected function clllLaterHandler() : void
        {
            if (this._info)
            {
                if (this._info.hasOwnProperty("name") && this._info["name"] != this._imgUrl)
                {
                    return;
                }
                if (this._info.bitmapData)
                {
                    this.bitmapData = this._info.bitmapData as BitmapData;
                }
                if (this.onLoadCompl != null)
                {
                    this.onLoadCompl.apply();
                }
                this._info = null;
            }
            return;
        }// end function

        public function get loadPriority() : int
        {
            return this._loadPriority;
        }// end function

        public function set loadPriority(param1:int) : void
        {
            this._loadPriority = param1;
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            x = 0;
            y = 0;
            this.onLoadCompl = null;
            this._info = null;
            this._loadPriority = 3;
            bitmapData = null;
            this.visible = true;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            LoaderManager.instance.removeResourceEvent(this._imgUrl, this.loadUrlCompl);
            this.imgUrl = "";
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

        public static function diposeInfo() : void
        {
            var _loc_1:String = null;
            var _loc_2:ResourceInfo = null;
            for (_loc_1 in _dicUrlRefer)
            {
                
                if (_dicUrlRefer[_loc_1] == 0)
                {
                    _loc_2 = ResourceManager.getInfoByName(_loc_1);
                    if (_loc_2)
                    {
                        _loc_2.dispose();
                    }
                    delete _dicUrlRefer[_loc_1];
                }
            }
            return;
        }// end function

        public static function getUrl(param1:String) : String
        {
            var _loc_3:String = null;
            var _loc_2:* = param1.toLocaleLowerCase();
            for each (_loc_3 in _arySuffix)
            {
                
                if (_loc_2.indexOf(_loc_3) > 0)
                {
                    return param1;
                }
            }
            return param1 + ".swf";
        }// end function

        public static function removeReference(param1:String) : void
        {
            if (_dicUrlRefer.hasOwnProperty(param1))
            {
                if (_dicUrlRefer[param1] > 0)
                {
                    var _loc_2:* = _dicUrlRefer;
                    var _loc_3:* = param1;
                    var _loc_4:* = _dicUrlRefer[param1] - 1;
                    _loc_2[_loc_3] = _loc_4;
                }
            }
            return;
        }// end function

        public static function addReference(param1:String) : void
        {
            if (_dicUrlRefer.hasOwnProperty(param1))
            {
                var _loc_2:* = _dicUrlRefer;
                var _loc_3:* = param1;
                var _loc_4:* = _dicUrlRefer[param1] + 1;
                _loc_2[_loc_3] = _loc_4;
            }
            else
            {
                _dicUrlRefer[param1] = 1;
            }
            return;
        }// end function

    }
}
